package com.qlh.camera

import javafx.scene.control.Alert
import java.io.BufferedReader
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.lang.StringBuilder
import java.text.SimpleDateFormat
import java.util.*
import javax.swing.JOptionPane
import kotlin.collections.HashMap

object FileUtil {

    //创建图片目录
    private val nameSdf = SimpleDateFormat("HH_mm_ss_SSS")
    private val parentSdf = SimpleDateFormat("yyyy-MM-dd")
    private val daskName = "D"//磁盘目录
    private val parentDocument = "cigarettes_cache"

    private val configPath = "$daskName:\\$parentDocument\\config\\config.txt"//配置参数
    private val resultPath = "$daskName:\\$parentDocument"

    fun getPicPath(): String {
        val date = Date()
        val fileName = "$daskName:\\$parentDocument\\${parentSdf.format(date)}\\${nameSdf.format(date).replace(":", "_")}.jpg"
        File(fileName).parentFile.apply {
            if (!this.exists()) {
                this.mkdirs()
            }
        }
        return fileName
    }

    //保存配置参数
    fun saveDefaultParams(ip: String, name: String, pwd: String, port: String) {
        File(configPath).parentFile.apply {
            if (!this.exists()) {
                this.mkdirs()
            }
        }

        File(configPath).apply {
            if (this.exists()) {
                this.delete()
            }
        }
        val sb = StringBuilder().apply {
            append("ip:$ip")
            append("\r\n")
            append("name:$name")
            append("\r\n")
            append("pwd:$pwd")
            append("\r\n")
            append("port:$port")
        }
        saveData(sb.toString(), configPath)
    }

    //获取保存的默认参数
    fun getDefaultParams(): HashMap<String, String> {
        val map = hashMapOf<String, String>()
        val configFile = File(configPath)
        if (configFile.exists()) {
            val rd = BufferedReader(FileReader(configFile))
            var line: String? = null
            while ({ line = rd.readLine();line }() != null) {
                println(line)
               line?.split(":")?.let {
                   map[it[0]] = it[1]
               }
            }
        }

        return map
    }

    //保存识别结果
    fun saveResult(data: String){
        val date = Date()
        val path = "$resultPath\\${parentSdf.format(date)}\\result.txt"
        File(path).parentFile.apply {
            if (!this.exists()) {
                this.mkdirs()
            }
        }
        saveData(data,path)
    }

    fun getHistory():List<ResultBO>{
        val result = arrayListOf<ResultBO>()
        val date = Date()
        val path = "$resultPath\\${parentSdf.format(date)}\\result.txt"
        val file = File(path)
        if (file.exists()){
            val rd = BufferedReader(FileReader(file))
            var line: String? = null
            while ({ line = rd.readLine();line }() != null) {
                println(line)
                line?.split(",")?.let {
                    val tmp = ResultBO(it[0],it[1],it[2],it[3])
                    result.add(tmp)
                }
            }
        }
        println("历史记录：${result.size}")
        return result
    }

    private fun saveData(data: String, filePath: String) {
        val resultWf = FileWriter(filePath, true)
        try {
            resultWf.write(data)
            //resultWf.write("\r\n")
            resultWf.flush()
        } catch (e: Exception) {
            e.printStackTrace()
        } finally {
            resultWf.close()
        }
    }
}